//
//  MBThirdLoginController.m
//  MiBaoTu
//
//  Created by 彭鹏 on 16/10/14.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBThirdLoginController.h"
#import "MBRelatingPhoneController.h"
#import "MBTabBarController.h"

#import "AFNetworking.h"
#import <UMMobClick/MobClick.h>

@interface MBThirdLoginController () {
    NSNumber        *num;
}

@property (strong, nonatomic) IBOutlet UIButton *login;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bgTop;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *titleLbLeft;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *titleLbTop;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *seletcedLeft;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *loginLeft;
@property (strong, nonatomic) IBOutlet UILabel *titleLb;
@property (strong, nonatomic) IBOutlet UILabel *selectedLb;
@property (strong, nonatomic) IBOutlet UIButton *selectedBtn;


@end

@implementation MBThirdLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleView:@"第三方登录"];
    [self setupNavBar];
    
    [self setupUI];
    
   
    
    
}

- (void)setupUI {
    self.login.layer.cornerRadius = 5.0;
    self.login.clipsToBounds = YES;
    _selectedBtn.selected = YES;
    if (ISIPAD) {
        
        _titleLb.font = [UIFont systemFontOfSize:15];
        _selectedLb.font = [UIFont systemFontOfSize:14];
        
        _bgTop.constant = 500*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        _titleLbTop.constant = 30*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        _titleLbLeft.constant = 600*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        _seletcedLeft.constant = 600*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        _loginLeft.constant =(SCREEN_WIDTH-700*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.))/2.;

    }

}
- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    if (ISIPAD) {
        
        _titleLb.font = [UIFont systemFontOfSize:15];
        _selectedLb.font = [UIFont systemFontOfSize:14];
        _bgTop.constant = 500*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        _titleLbTop.constant = 30*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        _titleLbLeft.constant = 600*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        _seletcedLeft.constant = 600*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        _loginLeft.constant = (SCREEN_WIDTH-700*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.))/2.;
    }
}

- (void)setupNavBar {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"fanhui"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(navigationBackButton)];
}


//返回
-(void)navigationBackButton {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)selected:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    
}

//授权并登录
- (IBAction)login:(id)sender {
    
    if (_selectedBtn.selected == NO) {
        [ErrorMessage showErrorMessage:@"请授权登录" inView:self.view];
        return;
    }
    num = @1;
    NSString *provider = @"QQ";
    switch (_type) {
        case ThirdLoginType_QQ:
        {
            num = @2;
            provider = @"QQ";
        }
            break;
        case ThirdLoginType_WX:
        {
            num = @1;
            provider = @"WX";
        }
            break;
        case ThirdLoginType_WB:
        {
            num = @3;
            provider = @"WB";
        }
            break;
            
        default:
            break;
    }
    
    //第三方登录判断
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:VERIFICATION_THIRDLOGIN_URL_PATH,num,_openID,_nickName,_picImgName] view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        //0 注册成功 1已经注册
        if ([json[@"ret_code"] intValue] == 0) {
            //注册成功 设置属性
            NSDictionary *data = json[@"data"];
            if (data.allKeys.count == 0) return ;
            
            UserModel *model = [UserModel objectWithKeyValues:data];
            [[UserManager standerDefult] setUserModel:model];
            
            //保留第三方登录信息
            NSDictionary *dic = @{@"openId":_openID,
                                  @"nickName":_nickName,
                                  @"picImg":_picImgName,
                                  @"type":num};
            [[NSUserDefaults standardUserDefaults]setObject:dic forKey:@"three_login"];
            [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:@"login_phone"];
            [[NSUserDefaults standardUserDefaults]synchronize];
            
            MBRelatingPhoneController * relatingPhoneVC = [[MBRelatingPhoneController alloc] init];
            [self.navigationController pushViewController:relatingPhoneVC animated:YES];
        }
        else if ([json[@"ret_code"] intValue] == 1) {
            //已注册
            NSDictionary *data = json[@"data"];
            if (data.allKeys.count == 0) return ;
            
            UserModel *model = [UserModel objectWithKeyValues:data];
            [[UserManager standerDefult] setUserModel:model];
            
            NSArray *collecitonArr = json[@"AudioIDList"];
            if ([collecitonArr isKindOfClass:[NSArray class]]) {
                [[UserManager standerDefult].collectionMusicArr removeAllObjects];
                [[UserManager standerDefult].collectionMusicArr addObjectsFromArray:collecitonArr];
            }
            NSArray *colleciton2Arr = json[@"AlbumIDList"];
            if ([colleciton2Arr isKindOfClass:[NSArray class]]) {
                [[UserManager standerDefult].collectionAlbumArr removeAllObjects];
                [[UserManager standerDefult].collectionAlbumArr addObjectsFromArray:colleciton2Arr];
            }
            NSArray *colleciton3Arr = json[@"ShowIDList"];
            if ([colleciton3Arr isKindOfClass:[NSArray class]]) {
                [[UserManager standerDefult].collectionShowArr removeAllObjects];
                [[UserManager standerDefult].collectionShowArr addObjectsFromArray:colleciton3Arr];
            }
            NSArray *praiseListArr = json[@"PraiseList"];
            if ([praiseListArr isKindOfClass:[NSArray class]]) {
                [[UserManager standerDefult].praiseListArr removeAllObjects];
                [[UserManager standerDefult].praiseListArr addObjectsFromArray:praiseListArr];
            }

            
            NSDictionary *dic = @{@"openId":_openID,
                                  @"nickName":_nickName,
                                  @"picImg":_picImgName,
                                  @"type":num};
            [[NSUserDefaults standardUserDefaults]setObject:dic forKey:@"three_login"];
            [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:@"login_phone"];
            [[NSUserDefaults standardUserDefaults]synchronize];
            
            [self onLoginOutBack];
        }
        
        //统计
        [MobClick profileSignInWithPUID:_openID provider:provider];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
   
    
}

//第三方登录成功后返回
- (void)onLoginOutBack
{
    if (_formBaseViewCtrl) {
        for (UIViewController *controller in self.navigationController.viewControllers) {
            if ([_formBaseViewCtrl isEqualToString:NSStringFromClass([controller class])]) {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    } else {
        MBTabBarController *tabBar = [[MBTabBarController alloc] init];
        [self presentViewController:tabBar animated:YES completion:nil];
    }
}

@end
